<template>
  <div class="login_container">
      <div class="login_box">
        <div class="avn_box">
            <img src="../assets/logo.png"/>
            <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                <el-form-item label="用户名" prop="username">
                    <el-input type="text" v-model="ruleForm.username" autocomplete="off" prefix-icon="el-icon-user" ></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input type="password" v-model="ruleForm.password" autocomplete="off" show-password prefix-icon="el-icon-lock"></el-input>
                </el-form-item>
                <el-form-item label="年龄" prop="age">
                    <el-input v-model.number="ruleForm.age" prefix-icon="el-icon-discover"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
                    <el-button @click="resetForm('ruleForm')">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
      </div>
  </div>
</template>

<script>
export default {
    data() {
      var checkAge = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('年龄不能为空'));
        }else{
          return callback()
        }
        setTimeout(() => {
          if (!Number.isInteger(value)) {
            callback(new Error('请输入数字值'));
          } else {
            if (value < 18) {
              callback(new Error('必须年满18岁'));
            } else {
              callback();
            }
          }
        }, 100);
      };
      var validatePass = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('密码不能为空'));
        }
        if (value === '') {
          return callback(new Error('请输入密码'));
        } 
        return callback()
      };
      
      var checkUsername=(rule, value, callback) =>{
        if(!value) return callback(new Error('用户名不能为空'))
        if(value === '') return callback(new Error("请输入用户名"))
        return callback()
      }

      return {
        ruleForm: {
          username: '',
          password: '',
          age: ''
        },
        rules: {
          password: [
            { validator: validatePass, trigger: 'blur' }
          ],
            age: [
            { validator: checkAge, trigger: 'blur' }
          ],
            username: [
            {validator: checkUsername,trigger:'blur'}
          ]
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate(async valid => {
          console.log(valid)
          if (valid) {
            const a=await this.$http.post("login",this.ruleForm)
            console.log(a)
            if (a.data.meta.status!==200) return  this.$message({type: 'error',message: '登录失败!'})
            this.$message({type: 'success',message: '登录成功!'});
            window.sessionStorage.setItem("token",a.data.data.token)//将用户的token信息保存到sessionStroage中
            //页面跳转
            this.$router.push("/home")
          } else {
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>

<style lang="less" scope>
.login_container{
    background-color: #153a5f;
    height: 100%;
}
.login_box{
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 3px;
    position: absolute;
    top:50%;
    left:50%;
    transform: translate(-50%,-50%);
    
}


</style>
